class AccountController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  # To require logins, use:
  #
  #   before_filter :login_required                            # restrict all actions
  #   before_filter :login_required, :only => [:edit, :update] # only restrict these actions
  # 
  # To skip this in a subclassed controller:
  #
  #   skip_before_filter :login_required

  # say something nice, you goof!  something sweet.
  
  def index
    redirect_to :action=>"login"
  end
  
  def send_email(from, from_alias, to, to_alias, subject, message)
  msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
  
#{message}
END_OF_MESSAGE
  
  Net::SMTP.start('star.math.spbu.ru', 25) do |smtp|
    smtp.send_message msg, from, to
  end
end

  
  def send_password
   if request.post?
   @user = User.new(params[:user])
      while @user.login do
        login,@user.login=@user.login.split(/\s*,\s*/)
      end
        if login!=nil     
            @cur_user = User.find_by_login(login)
          if @cur_user != nil 
            
          new_password = 100000+rand(1000000)
          send_email('m05mvv@star.math.spbu.ru', 'RubyPhotoGallery', @cur_user.email, @cur_user.first+'.'+@cur_user.last, 'New_password', 'Your new password is:'+String(new_password))
          @cur_user.crypted_password = @cur_user.encrypt(new_password)
          @cur_user.save
          flash[:notice] = _("Message has been sent")
     
          
          else
            flash[:notice] = _("Unknown user! Choose another login")
          end  
        else
            flash[:notice] = _("Please enter your login")
        end
   end
  end

  def login
    redirect_to(:action => 'signup') and return unless User.count > 0
    return unless request.post?
    @user = User.new(params[:user])
    self.current_user = User.authenticate(@user.login, @user.password)
    if current_user
      redirect_back_or_default(:controller => 'admin', :action => 'index')
      flash[:notice] = _("Logged in successfully")
    else 
      flash[:notice] = _("Incorrect Login/Password")
    end
  end

  def signup
    redirect_to(:action=>"login") and return unless logged_in? or User.count < 1
    @user = User.new(params[:user])
    return unless request.post?
    if @user.save
      redirect_back_or_default(:controller => 'admin', :action => 'index')
      flash[:notice] = _("Thanks for signing up!")
    end
  end
  

  
  # Sample method for activating the current user
  #def activate
  #  @user = User.find_by_activation_code(params[:id])
  #  if @user and @user.activate
  #    self.current_user = @user
  #    redirect_back_or_default(:controller => '/account', :action => 'index')
  #    flash[:notice] = "Your account has been activated."
  #  end
  #end

  def logout
    self.current_user = nil
    flash[:notice] = _("You have been logged out.")
    redirect_back_or_default(:controller => 'view', :action => 'show_albums')
  end
end
